package com.situ.company.project.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.situ.company.project.model.ProjectModel;
import com.situ.company.util.JDBCUtil;

public class ProjectDao {
	private static String table = "st_company.project";
	private static String cols = "code,name,time";
	private Connection conn = null;
	private PreparedStatement ps = null;
	
	public Integer insert(ProjectModel model) {
		StringBuffer sql = new StringBuffer("insert into ");
		sql.append(table).append(" (").append(cols).append(") values(?,?,?)");
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, model.getCode());
			ps.setString(2, model.getName());
			ps.setString(3, model.getTime());
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps);
		}
		return null;
	}

	public Integer delete(ProjectModel model) {
		StringBuffer sql = new StringBuffer("delete from ");
		sql.append(table);// where code = ?
		List<Object> vals = appendWhere(sql, model);
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString());
			for (int i = 0; i < vals.size(); i++) {
				ps.setObject(i + 1, vals.get(i));
			}
			return ps.executeUpdate();
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps);
		}
		return null;
	}

	public List<ProjectModel> selectList(ProjectModel model) {
		List<ProjectModel> list = new ArrayList<>();
		StringBuffer sql = new StringBuffer("select ");
		sql.append(cols).append(" from ").append(table);
		List<Object> vals = appendWhere(sql, model);
		System.out.println(sql);

		ResultSet rs = null;
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString());
			for (int i = 0; i < vals.size(); i++) {
				ps.setObject(i + 1, vals.get(i));
			}
			rs = ps.executeQuery();
			while (rs.next()) {
				ProjectModel mod = new ProjectModel();
				mod.setCode(rs.getString("code"));
				mod.setName(rs.getString("name"));
				mod.setTime(rs.getString("time"));
				list.add(mod);
			}
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}
		return list;
	}

	private List<Object> appendWhere(StringBuffer sql, ProjectModel model) {
		sql.append(" where 1=1");
		List<Object> objs = new ArrayList<Object>();
		String code = model.getCode();
		if (code != null && !code.trim().isEmpty()) {// !"".equals(code.trim())
			sql.append(" and code like ?");
			objs.add(code);
		}
		String name = model.getName();
		if (name != null && !name.trim().isEmpty()) {
			sql.append(" and name like ?");
			objs.add(name);
		}
		if (model.isPageOn()) {
			sql.append(" limit ?,? ");
			objs.add(model.getRowStart());
			objs.add(model.getRowCount());
		}
		return objs;
	}

	public Integer update(ProjectModel model) {
		StringBuffer sql = new StringBuffer("update ");
		sql.append(table);// set name=?,tel=? where code=?
		List<Object> vals = appendSet(sql, model);
		System.out.println(sql);
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString());
			for (int i = 0; i < vals.size(); i++) {
				ps.setObject(i + 1, vals.get(i));
			}
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps);
		}
		return null;
	}

	private List<Object> appendSet(StringBuffer sql, ProjectModel model) {
		sql.append(" set id=id ");
		List<Object> objs = new ArrayList<Object>();

		String name = model.getName();
		if (name != null) {
			sql.append(",name = ? ");
			objs.add(name);
		}
		String time = model.getTime();
		if (time != null) {
			sql.append(",time = ? ");
			objs.add(time);
		}
		String code = model.getCode();
		sql.append("where code = ?");
		objs.add(code);
		return objs;
	}
	
	public int selectCount(ProjectModel model) {

		StringBuffer sql = new StringBuffer("select count(id) from ");
		sql.append(table);
		List<Object> vals = appendWhere(sql, model);

		System.out.println(sql);

		ResultSet rs = null;
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString());
			for (int i = 0; i < vals.size(); i++) {
				ps.setObject(i + 1, vals.get(i));	
			}
			rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}
		return 0;
	}
}
